package com.novasoftware.novagooglemap.db.builder;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.util.Log;

import com.novasoftware.novagooglemap.db.DataBaseBuilder;
import com.novasoftware.novagooglemap.db.entity.Student;;

public class StudentBuilder  implements DataBaseBuilder<Student> {
	
	private String root;
	
	public String getRoot() {
		return this.root;
	}

	public void setRoot(String root) {
		this.root = root;
	}

	public String table() {
		return "Student";
	}

	@Override
	public Student build(Cursor query) {
		Log.i("Student Build", "Begin");
		int Id = query.getColumnIndex("Id");
		int FirstName = query.getColumnIndex("FirstName");
		int LastName = query.getColumnIndex("LastName");
		int Sex = query.getColumnIndex("Sex");
		int Age = query.getColumnIndex("Age");
		int Phone = query.getColumnIndex("Phone");
		int Address = query.getColumnIndex("Address");
		int GeoPoint = query.getColumnIndex("GeoPoint");
		int CreatedOn = query.getColumnIndex("CreatedOn");
		
		Student student = new Student();
		student.Id = query.getInt(Id);
		student.FirstName = query.getString(FirstName);
		student.LastName = query.getString(LastName);
		student.Sex = query.getString(Sex);
		student.Age = query.getInt(Age);
		student.Phone = query.getString(Phone);
		student.Address = query.getString(Address);
		student.GeoPoint = query.getString(GeoPoint);
		student.CreatedOn = query.getString(CreatedOn);
		Log.i("Student Build", "End");
		return student;
	}

	@Override
	public ContentValues deconstruct(Student entity) {
		ContentValues values = new ContentValues();
		
	    if(entity.Id>0)	values.put("Id", entity.Id);
		values.put("FirstName", entity.FirstName);
		values.put("LastName", entity.LastName);
		values.put("Sex", entity.Sex);
		values.put("Age", entity.Age);
		values.put("Phone", entity.Phone);
		values.put("Address", entity.Address);
		values.put("GeoPoint", entity.GeoPoint);
		values.put("CreatedOn", entity.CreatedOn);
		return values;
	}

	@Override
	public Student jsonBuild(JSONObject jsonObject) throws JSONException {
		Student student = new Student();
		student.Id = jsonObject.getInt(root+"Id");
		student.FirstName = jsonObject.getString(root+"FirstName");
		student.Sex = jsonObject.getString(root+"Sex");
		student.Age = jsonObject.getInt(root+"Age");
		student.Phone = jsonObject.getString(root+"Phone");
		return student;
	}


}
